﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using System.Data;
using System.Data.OleDb;

public class SiteStatistics
{

}

public partial class _Default : System.Web.UI.Page 
{
    private const string _connectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source={0}";
    private const string _selectCountCommand = "SELECT Count(*) FROM [zstblSiteStatistics] where [ObjectName] = '{0}'";
    private const string _insertCommand = "INSERT INTO [zstblSiteStatistics] ([ObjectName], [AccessCount]) VALUES ('{0}', {1})";
    private const string _updateCommand = "UPDATE [zstblSiteStatistics] SET [AccessCount] = [AccessCount] + 1 WHERE [ObjectName] = '{0}'";

    public void UpdateObjectStats(string objectName)
    {
        string mdb = Server.MapPath(AccessDataSource1.DataFile);    
        string connectionString =
            string.Format(_connectionString, mdb);

        using (OleDbConnection cnn = new OleDbConnection(connectionString))
        {
            cnn.Open(); 
            string sql = string.Format(_selectCountCommand, objectName);  
            OleDbCommand cmd = new OleDbCommand(sql, cnn);
            int count = (int)cmd.ExecuteScalar();
            if (count == 0)
            {
                sql = string.Format(_insertCommand, objectName, 0);
                cmd = new OleDbCommand(sql, cnn);
                cmd.ExecuteNonQuery(); 
            }
            sql = string.Format(_updateCommand, objectName);
            cmd = new OleDbCommand(sql, cnn);
            cmd.ExecuteNonQuery(); 

        }

    }

    protected void Page_Load(object sender, EventArgs e)
    {
         

    }

    private string makeReportUrl(string aspx)
    {
        UpdateObjectStats(aspx);
        return "~/ReportPages/" + aspx;
    }

    protected void LinkButton1_Click(object sender, EventArgs e)
    {
        Response.Redirect(makeReportUrl("AlphabeticalListOfProductsReport.aspx"));  
    }
    protected void LinkButton2_Click(object sender, EventArgs e)
    {
        Response.Redirect(makeReportUrl("CatalogReport.aspx"));  
    }
    protected void LinkButton3_Click(object sender, EventArgs e)
    {
        Response.Redirect(makeReportUrl("CustomersLabelsReport.aspx"));  

    }
    protected void LinkButton4_Click(object sender, EventArgs e)
    {
        Response.Redirect(makeReportUrl("EmployeeSalesByCountryReport.aspx"));  

    }
    protected void LinkButton5_Click(object sender, EventArgs e)
    {
        Response.Redirect(makeReportUrl("InvoiceReport.aspx"));  

    }
    protected void LinkButton6_Click(object sender, EventArgs e)
    {
        Response.Redirect(makeReportUrl("ProductByCategoryReport.aspx"));
    }
    protected void LinkButton7_Click(object sender, EventArgs e)
    {
        Response.Redirect(makeReportUrl("SalesAnalysisReport.aspx"));
    }
    protected void LinkButton8_Click(object sender, EventArgs e)
    {
        Response.Redirect(makeReportUrl("SalesByCategoryReport.aspx"));
    }
    protected void LinkButton9_Click(object sender, EventArgs e)
    {
        Response.Redirect(makeReportUrl("SalesByYearReport.aspx"));
    }
    protected void LinkButton10_Click(object sender, EventArgs e)
    {
        Response.Redirect(makeReportUrl("SalesTotalsByAmountReport.aspx"));

    }
    protected void LinkButton11_Click(object sender, EventArgs e)
    {
        Response.Redirect(makeReportUrl("ShippersReport.aspx"));

    }
    protected void LinkButton12_Click(object sender, EventArgs e)
    {
        Response.Redirect(makeReportUrl("SummaryOfSalesByQuarterReport.aspx"));
    }
    protected void LinkButton13_Click(object sender, EventArgs e)
    {
        Response.Redirect(makeReportUrl("SummaryOfSalesByYearReport.aspx"));

    }
}
